﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections;

namespace RCA1._0
{
	public class LOAICONGTYDAO: DataProvider
	{
		public DataTable LayBang()
		{
			OleDbConnection cn = this.Connect();
			string strSQL = "select * from LOAICONGTY";
			OleDbDataAdapter da = new OleDbDataAdapter(strSQL, cn);
			DataTable dt = new DataTable();
			da.Fill(dt);
			cn.Close();
			return dt;
		}

        #region "Lấy danh sách"
        
        public List<LOAICONGTYDTO> LayDanhSach()
		{
			OleDbConnection cn = this.Connect();
			string strSQL = "select * from LOAICONGTY";
			OleDbCommand cmd = new OleDbCommand(strSQL, cn);
			OleDbDataReader dr = cmd.ExecuteReader();
			LOAICONGTYDTO tempDTO;
			List<LOAICONGTYDTO> dsLOAICONGTY = new List<LOAICONGTYDTO>();
			while (dr.Read())
			{
				tempDTO = new LOAICONGTYDTO();
				tempDTO.IDLoaiCongTy = Convert.ToInt32(dr["IDLoaiCongTy"]);
				tempDTO.TenLoaiCongTy = dr["TenLoaiCongTy"].ToString();
				dsLOAICONGTY.Add(tempDTO);
			}
			return dsLOAICONGTY;
		}

        #endregion

        
        #region "Lấy danh sách mã loại"

        public List<LOAICONGTYDTO> LayMaLoaiCongTy()
        {
            OleDbConnection cn = this.Connect();
            string strSQL = "select * from LOAICONGTY";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr = cmd.ExecuteReader();
            LOAICONGTYDTO tempDTO;
            List<LOAICONGTYDTO> dsLOAICONGTY = new List<LOAICONGTYDTO>();
            while (dr.Read())
            {
                tempDTO = new LOAICONGTYDTO();
                tempDTO.IDLoaiCongTy = Convert.ToInt32(dr["IDLoaiCongTy"]);
                dsLOAICONGTY.Add(tempDTO);
            }
            return dsLOAICONGTY;
        }

        #endregion


        #region "Tìm kiếm"
        public LOAICONGTYDTO TimTheoMa(int MaLoaiCongTy)
		{
			OleDbConnection cn = this.Connect();
			string strSQL = "select * from LOAICONGTY Where IDLoaiCongTy=" + MaLoaiCongTy.ToString();
			OleDbCommand cmd = new OleDbCommand(strSQL, cn);
			OleDbDataReader dr = cmd.ExecuteReader();
			dr.Read();
			LOAICONGTYDTO tempDTO = new LOAICONGTYDTO();
			tempDTO.IDLoaiCongTy = Convert.ToInt32(dr["IDLoaiCongTy"]);
			tempDTO.TenLoaiCongTy = dr["TenLoaiCongTy"].ToString();
			cn.Close();			return tempDTO;
		}

        #endregion


        #region "Thêm"

        public void Them(LOAICONGTYDTO tempDTO)
		{
			OleDbConnection cn = this.Connect();
			string strSQL = "insert into LOAICONGTY(TenLoaiCongTy) values (@TenLoaiCongTy)";
			OleDbCommand cmd = new OleDbCommand(strSQL, cn);
			cmd.Parameters.Add("@TenLoaiCongTy", OleDbType.LongVarWChar).Value = tempDTO.TenLoaiCongTy;
			cmd.ExecuteNonQuery();
			cn.Close();
		}

        #endregion
        

        #region "Xóa"

        public void Xoa(int MaLoaiCongTy)
		{
			OleDbConnection cn = this.Connect();
			string strSQL = "delete from LOAICONGTY Where IDLoaiCongTy = "+MaLoaiCongTy.ToString();
			OleDbCommand cmd = new OleDbCommand(strSQL, cn);
			cmd.ExecuteNonQuery();
			cn.Close();
		}

        #endregion


        #region "Sửa"

        public void Sua(LOAICONGTYDTO tempDTO)
		{
			OleDbConnection cn = this.Connect();
			string strSQL = "update LOAICONGTY set TenLoaiCongTy = @TenLoaiCongTy where IDLoaiCongTy = @IDLoaiCongTy";
			OleDbCommand cmd = new OleDbCommand(strSQL, cn);
			cmd.Parameters.Add("@TenLoaiCongTy", OleDbType.LongVarWChar).Value = tempDTO.TenLoaiCongTy;
			cmd.Parameters.Add("@IDLoaiCongTy", OleDbType.Integer).Value = tempDTO.IDLoaiCongTy;
			cmd.ExecuteNonQuery();
			cn.Close();
        }

        #endregion

    }
}